<template>
	<view>
		<view class="top">
			<up-image :src="bg" width="100%" height="700rpx"></up-image>
			
			<view class="info">
				<up-image :src="logo" height="120rpx" mode="heightFix" class="logo"></up-image>
				
				<view class="product">数字工会平台</view>
			</view>
		</view>
		
		
		
		<view class="form">
			<!-- <view style="display: flex;align-items:center;position: relative;margin-bottom: 30rpx;">
			    <up-input v-model="loginForm.orgName"  @click="showSelect = true" placeholder=""/>
			    <up-select v-model="showSelect" :options="cateList" @select="selectItem" style="position: absolute;left: 0;width: 100%;padding-left: 20rpx;color: #ccc;" label="工会"></up-select>
			</view> -->
			
			<view style="margin-bottom: 30rpx;">
			    <up-picker
			        ref="uPickerRef"
			        :show="showPicker"
			        :columns="dataList"
			        @confirm="confirm"
			        @change="changeHandler"
			        @cancel="showPicker = false"
			      ></up-picker>
			    <up-button @click="showPicker = true">选择工会</up-button>
				<up-input v-model="loginForm.orgName"></up-input>
			</view>
			
			<up-input placeholder="请输入姓名" v-model="loginForm.memberName">
				  <template #prefix>
					<up-text
					  margin="0 3px 0 0"
					  type="tips"
					></up-text>
				  </template>
			</up-input>
			
			<!-- <up-input placeholder="请输入手机号" v-model="loginForm.mobile">
				  <template #prefix>
					<up-text
					  margin="0 3px 0 0"
					  type="tips"
					></up-text>
				  </template>
			</up-input> -->
			
			<!-- <up-input placeholder="请输入密码" type="password" v-model="loginForm.password">
			</up-input> -->
			
			<view class="forget-wrap">
				<!-- <text>忘记密码?</text> -->
			</view>
			
			<up-button type="primary" text="注册" color="rgba(246, 0, 0, 1)" shape="circle" @click="register"></up-button>
			
			<up-checkbox
				:customStyle="{marginTop: '30rpx'}"
				name="agree"
				usedAlone
				@change="handleCheckboxChange"
			>
				<template #label>
					<text class="common">我已阅读并同意</text>
					<text class="highlight">《用户服务协议》</text>
					<text class="common">和</text>
					<text class="highlight">《隐私政策》</text>
				</template>
			</up-checkbox>
		</view>
	</view>
</template>

<script>
	import bg from '@/static/images/login/bg.png'
	import logo from '@/static/images/logo.png'
	import { orgApi } from '@/api/org/org-api.js';
	import { sm2Encrypt } from '@/utils/smCrypto';
	import { wxLogin,showConfirm  } from '@/utils/common'; 
	import {loginApi} from '@/api/login/login-api.js';
	
	export default {
		data() {
			return {
				bg,
				logo,
				isAgree: false, // 初始状态为未选中
				loginForm:{
					memberName: '',
					password: '',
				},
				cateList:[],
				memberPhone:'',
				showPicker:false,
				dataList:[
				]
				
			}
		},
		onLoad(options) {
			this.getOrgList();
			this.memberPhone = options.memberPhone;
		},
		methods: {
			handleCheckboxChange(event) {
				this.isAgree = event;
			},
			async register(){
				if(!this.isAgree){
					uni.showToast({
						icon: 'none',
						title: '请阅读并同意《用户服务协议》、《隐私政策》',
					});
					return;
				}
				const loginForm = this.loginForm;
				if (!loginForm.memberName) {
					uni.showToast({
						icon: 'none',
						title: '请输入用户名',
					});
					return;
				}
				// if (!loginForm.mobile) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请输入手机号',
				// 	});
				// 	return;
				// }
				
				// if (!loginForm.password) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请输入密码',
				// 	});
				// 	return;
				// }
				// #ifdef MP-WEIXIN
				const code = await wxLogin();
				loginForm.code = code;
				loginForm.mobile = this.memberPhone;
				// #endif
				
				
				// #ifdef H5
				const app = getApp();
				const userInfo = app.globalData.currentUser;
				loginForm.mobile = userInfo.memberPhone;
				if(!userInfo){
					uni.showToast({
						title:'未获取到用户的微信信息',
						icon:'error'
					})
					return;
				}
				loginForm.unionId = userInfo.unionid;
				loginForm.openId = userInfo.openId;
				// #endif
				
				loginApi.register(loginForm).then(res=>{
					uni.showToast({
						title:'注册成功'
					})
					uni.navigateTo({
						url: '/pages/user/mobile-login'
					})
				}).catch(error=>{
					uni.showToast({
						title:error
					})
				})
				
			},
			getOrgList(){
				orgApi.queryList().then(res=>{
					this.cateList = res.data;
					let list = [];
					for (var i = 0; i < this.cateList.length; i++) {
						let item = this.cateList[i];
						list.push(item.name);
					}
					this.dataList.push(list);
				});
				
			},
			selectItem(e){
				//console.log(e);
				this.loginForm.orgName = e.name;
				this.loginForm.orgId = e.id;
			},
			confirm(e){
				const index = e.indexs[0];
				const item = this.cateList[index];
				this.loginForm.orgId = item.id;
				this.loginForm.orgName = item.name;
				this.showPicker = false;
			}
			
		}
	}
</script>

<style lang="scss" scoped>
@import '@/uni.scss';

.top {
	position: relative;
	.info {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	
	.logo {
		top: 100rpx;
	}
	
	.product {
		padding: 30rpx 0;
		font-size: 48rpx;
		font-weight: bold;
		color: #fff;
	}
}

.form {
	padding: 60rpx;
	
	.u-input + .u-input {
		margin-top: 30rpx;
	}
	
	.forget-wrap {
		font-size: 28rpx;
		padding: 30rpx 0;
		text-align: right;
		color: $uni-text-color-grey;
	}
	
	.common {
		font-size: 28rpx;
		color: $uni-text-color-grey;
	}
	
	.highlight {
		font-size: 28rpx;
		color: $uni-color-info;
	}
	
	.tourist {
		font-size: 28rpx;
		padding: 20rpx 0;
		text-align: center;
		color: $uni-text-color-grey;
	}
}


</style>
